package com.application.chiettinhdienluc.control;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.application.chiettinhdienluc.model.HinhAnhChiTietModel;

public class HinhAnhInfoListControl extends ArrayList<HinhAnhChiTietModel> {

	private static final long serialVersionUID = 2963755199959921000L;
	private volatile HashMap<String, HinhAnhChiTietModel> hashHinhAnhInfo = new HashMap<String, HinhAnhChiTietModel>();

	@Override
	public void clear() {
		hashHinhAnhInfo.clear();		
		super.clear();
	}

	@Override
	public boolean add(HinhAnhChiTietModel object) {
		boolean value = false;
		if(hashHinhAnhInfo.put(object.getFilename(), object) == null) {
			value = super.add(object);
		}
		return value;
	}

	@Override
	public boolean addAll(Collection<? extends HinhAnhChiTietModel> collection) {
		boolean value = false;
		for (HinhAnhChiTietModel hinhAnhInfo : collection) {
			if(hinhAnhInfo != null)
			{
				if (hashHinhAnhInfo.put(hinhAnhInfo.getFilename(), hinhAnhInfo) == null) {
					value = true;
					value = super.add(hinhAnhInfo);
				}
			}
		}
		return value;
	}

	public int updateOrAdd(List<HinhAnhChiTietModel> listFriend) {
		int resultValue = 0;
		for (HinhAnhChiTietModel obj : listFriend) {

			if (contain(obj.getFilename())) {
				HinhAnhChiTietModel objOld = get(obj.getFilename());
				resultValue++;
			} else {
				try {
					hashHinhAnhInfo.put(obj.getFilename(), obj);
					resultValue++;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return resultValue;
	}


	@Override
	public HinhAnhChiTietModel remove(int index) {
		hashHinhAnhInfo.remove(get(index).getFilename());
		return super.remove(index);
	}

	@Override
	public boolean remove(Object object) {
		if (object instanceof HinhAnhChiTietModel) {
			hashHinhAnhInfo.remove(((HinhAnhChiTietModel) object).getFilename());
		}
		return super.remove(object);
	}

	public void clearFriendListCache() {

	}

	public boolean contain(String uid) {
		return hashHinhAnhInfo.containsKey(uid);
	}

	public HinhAnhChiTietModel get(String uid) {
		return hashHinhAnhInfo.get(uid);
	}
}
